//
//  TTCoreCommon.m
//  Tint
//
//  Created by Kevin Wu on 8/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTCoreCommon.h"


#pragma mark - Compatibility

TTDeviceModel TTCurrentDeviceModel()
{
    // TODO
    return TTDeviceModelIphone;
}

CGFloat TTCurrentOSVersion()
{
    UIDevice *device = [UIDevice currentDevice];
    return [device.systemVersion floatValue];
}



#pragma mark - Paths

NSString *TTDocumentsPath()
{
    static NSString *path = nil;
    if ( path == nil ) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        if ( [paths count] > 0 ) {
            path = [[NSString alloc] initWithString:[paths objectAtIndex:0]];
        }
    }
    return path;
}

NSString *TTLibraryPath()
{
    static NSString *path = nil;
    if ( path == nil ) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        if ( [paths count] > 0 ) {
            path = [[NSString alloc] initWithString:[paths objectAtIndex:0]];
        }
    }
    return path;
}

NSString *TTCachesPath()
{
    static NSString *path = nil;
    if ( path == nil ) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        if ( [paths count] > 0 ) {
            path = [[NSString alloc] initWithString:[paths objectAtIndex:0]];
        }
    }
    return path;
}



#pragma mark - Image cache

UIImage *TTCachedImage(NSString *name)
{
    return [UIImage imageNamed:name]; // AUTO
}

UIImage *TTUncachedImage(NSString *name)
{
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    return [UIImage imageWithContentsOfFile:path]; // AUTO
}

UIImage *TTCreateUncachedImage(NSString *name)
{
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    return [[UIImage alloc] initWithContentsOfFile:path];
}



#pragma mark - Validity

BOOL TTIsInstance(id object, Class cls)
{
    return ((object) && 
            [object isKindOfClass:cls]
            );
}

BOOL TTIsStringWithText(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSString class]] && 
            ([(NSString *)object length] > 0)
            );
}

BOOL TTIsDataWithBytes(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSData class]] && 
            ([(NSData *)object length] > 0)
            );
}

BOOL TTIsArrayWithItems(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSArray class]] && 
            ([(NSArray *)object count] > 0)
            );
}

BOOL TTIsDictionaryWithItems(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSDictionary class]] && 
            ([(NSDictionary *)object count] > 0)
            );
}

BOOL TTIsSetWithItems(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSSet class]] && 
            ([(NSSet *)object count] > 0)
            );
}



#pragma mark - RFC 3339 date

NSDateFormatter *TTRFC3339DateFormatter()
{
    static NSDateFormatter *dateFormatter = nil;
    
    if ( dateFormatter == nil ) {
        dateFormatter = [[NSDateFormatter alloc] init];
        
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
        [dateFormatter setLocale:locale];
        [locale release];
        
        [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    }
    return dateFormatter;
}

NSDate *TTDateFromRFC3339DateString(NSString *string)
{
    if ( string ) {
        return [TTRFC3339DateFormatter() dateFromString:string]; // AUTO
    }
    return nil;
}

NSString *TTRFC3339DateStringFromDate(NSDate *date)
{
    if ( date ) {
        return [TTRFC3339DateFormatter() stringFromDate:date];
    }
    return nil;
}



#pragma mark - Non-retaining collections

NSMutableArray *TTCreateNonRetainingMutableArray()
{
    return (NSMutableArray *)CFArrayCreateMutable(nil, 0, nil);
}

NSMutableDictionary *TTCreateNonRetainingMutableDictionary()
{
    return (NSMutableDictionary *)CFDictionaryCreateMutable(nil, 0, nil, nil);
}

NSMutableSet *TTCreateNonRetainingMutableSet()
{
    return (NSMutableSet *)CFSetCreateMutable(nil, 0, nil);
}

